home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Environments / Small Eiffel 0.4.8 / lib_std / arrayed_collection.e < prev    next >
Encoding:
Text File  |  1997-04-13  |  2.7 KB  |  144 lines  |  [TEXT/ttxt]

  1. -- Part of SmallEiffel -- Read DISCLAIMER file -- Copyright (C) 
  2. -- Dominique COLNET and Suzanne COLLIN -- colnet@loria.fr
  3. --
  4. deferred class ARRAYED_COLLECTION[E]
  5.    -- 
  6.    -- Common root for ARRAY[E] and FIXED_ARRAY[E].
  7.    --
  8.  
  9. inherit COLLECTION[E];
  10.  
  11. feature {ARRAYED_COLLECTION}
  12.    
  13.    storage: C_ARRAY[E];
  14.      -- Internal access to storage location.
  15.    
  16. feature
  17.    
  18.    capacity: INTEGER;
  19.      -- Internal storage capacity in number of item.
  20.    
  21.    upper: INTEGER;
  22.      -- Upper index bound.
  23.    
  24. feature -- 
  25.  
  26.    sub_array(low, up: INTEGER): like Current is
  27.       require
  28.      valid_index(low);
  29.      valid_index(up);
  30.      low <= up
  31.       deferred
  32.       ensure
  33.      same_type(Result);
  34.      Result.count = (up - low + 1);
  35.      Result.lower = low or Result.lower = 0
  36.       end;
  37.  
  38. feature -- Implementation of deferred :
  39.  
  40.    add(element: like item; index: INTEGER) is
  41.       do
  42.      if index = upper + 1 then
  43.         add_last(element);
  44.      else
  45.         add_last(element);
  46.         move(index,upper - 1,1);
  47.         put(element,index);
  48.      end;
  49.       end;
  50.  
  51.    remove_last is
  52.       do
  53.      upper := upper - 1;
  54.       end;
  55.    
  56. feature -- The Guru section :
  57.  
  58.    free is
  59.       local
  60.      mem: MEMORY;
  61.       do
  62.      if storage.is_not_void then
  63.         storage.free;
  64.      end;
  65.      mem.free(Current.to_pointer);
  66.       end;
  67.    
  68. feature -- Interfacing with C :
  69.    
  70.    to_external: POINTER is
  71.      -- Gives C access into the internal `storage' of the ARRAY.
  72.      -- Result is pointing the element at index `lower'.
  73.      -- 
  74.      -- NOTE: do not free/realloc the Result. Resizing of the array 
  75.      --       can makes this pointer invalid. 
  76.       require
  77.      not empty
  78.       do
  79.      Result := storage.to_pointer;
  80.       ensure
  81.      Result.is_not_void
  82.       end;
  83.  
  84. feature {NONE}
  85.  
  86.    malloc(size: INTEGER): POINTER is
  87.       require
  88.      size > 0
  89.       local
  90.      x: like item;
  91.      mem: MEMORY;
  92.       do
  93.      if x.is_expanded_type then
  94.         Result := mem.malloc(size * x.object_size);
  95.      else
  96.         Result := mem.malloc(size * mem.pointer_size);
  97.      end;
  98.       end;
  99.  
  100.    calloc(size: INTEGER): POINTER is
  101.       require
  102.      size > 0
  103.       local
  104.      x: like item;
  105.      mem: MEMORY;
  106.       do
  107.      if x.is_expanded_type then
  108.         Result := mem.calloc(size,x.object_size);
  109.      else
  110.         Result := mem.calloc(size,mem.pointer_size);
  111.      end;
  112.       end;
  113.  
  114.    realloc(pointer: POINTER; size: INTEGER): POINTER is
  115.       require
  116.      size > 0
  117.       local
  118.      x: like item;
  119.      mem: MEMORY;
  120.       do
  121.      if x.is_expanded_type then
  122.         Result := mem.realloc(pointer,size * x.object_size);
  123.      else
  124.         Result := mem.realloc(pointer,size * mem.pointer_size);
  125.      end;
  126.       end;
  127.  
  128. feature {ARRAYED_COLLECTION}
  129.  
  130.    set_upper(new_upper: like upper) is
  131.       do
  132.      upper := new_upper;
  133.       end;
  134.  
  135. invariant
  136.  
  137.    capacity >= 0;
  138.  
  139.    capacity >= (upper - lower + 1);
  140.  
  141.    capacity > 0 implies storage.is_not_void;
  142.  
  143. end -- ARRAYED_COLLECTION[E]
  144.